require 'serverside'
include ServerSide

DB.connect(:type => :postgres, :db => 'urls', 
  :user => 'postgres', :password => '12345678')
  
class UrlzController < Controller::REST.mount('/')
  def get
    @urls = DB[:urls].select(:order => 'stamp DESC')
    render_template('list')
  end
  
  def post
    DB[:urls].insert(:stamp => Time.now,
      :title => @parameters[:title], :url => @parameters[:url])
    redirect('/')
  end
end

list_template = <<EOT
<ul>
  <% for item in @urls do %>
    <li><a href="<%= item[:title] %>"><%= item[:url] %></a></li>
  <% end %>
</ul>
EOT

Template.set('list', list_template)
